package com.controller.manage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.Permission;
import com.po.Role;
import com.service.manage.RoleService;
import com.util.UUIDGenerator;

@Controller
@RequestMapping("/jsp/manager")
public class RoleController {
	@Resource(name="roleServiceImp")
	public RoleService roleService;
	@ResponseBody
	@RequestMapping(value = "/getRoleListN")
	public List<Role> getRoleYList() {
		List<Role> list = roleService.getRoleListN();
		return list;
	}
	@ResponseBody
	@RequestMapping(value = "/getRoleID")
	public Role getRoleID(String name) {
		String id = roleService.getRoleID(name);
		Role role=new Role();
		role.setRoleid(id);
		role.setRolename(name);
		return role;
	}
	@ResponseBody
	@RequestMapping(value="/getRoleList")
	public Map<String,Object> getRoleList(Role role,Integer page,Integer rows){
		
		role.setPage((page-1)*rows);
		role.setRows(rows);
		List<Role> list = roleService.getRole(role);
		int count = roleService.getRoleCount(role);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", count);
		map.put("rows", list);
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/addRole")
	public int addRole(Role role){
		role.setRoleid(UUIDGenerator.getUUID());
		role.setRoletime(new SimpleDateFormat("yyyy-MM-dd  hh:mm").format(new Date()));
		int result = roleService.addRole(role);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateRole")
	public int updateRole(Role role){
		
		int result = roleService.updateRole(role);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/delRole")
	public int deleteRole(Role role){
		
		int result = roleService.delRole(role);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/delRoleList")
	public int deleteRoles(String roleids){
		
		String[] array = roleids.split(",");
		int result = roleService.delRoles(array);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/getRolePermission")
	public Map<String,Object> getRolePermission(Role role,Integer page,Integer rows){
		
		role.setPage((page-1)*rows);
		role.setRows(rows);
		List<Role> list = roleService.getRolePermission(role);
		int count = roleService.getRoleCount(role);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("rows", list);
		map.put("total", count);
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/delRolePermission")
	public int delRolePermission(String roles){
		
		//String[] roleids = roles.split(",");
		//int result = roleService.delRolePermission(roleids);
		return 0;
	}
	
	@ResponseBody
	@RequestMapping(value="/addRolePermission",method=RequestMethod.POST, 
		    consumes="application/json")
	public int addRolePermission(@RequestBody List<Role> roles){
		roleService.delRolePermission(roles.get(0));
		int result = roleService.addRolePermission(roles);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/getRPermissionList")
	public List<Permission> getPermissionList(){
		
		List<Permission> list = roleService.getPermissionList();
		return list;
	}
	
	@ResponseBody
	@RequestMapping(value="/getRolePermissionList")
	public List<Role> getRolePermissionList(Role role){
		System.out.println(role.getRoleid());
		List<Role> list = roleService.getRolePermissionList(role);
		return list;
	}
}
